package com.cg.service;

import com.cg.common.MailCommon;
import com.cg.dto.NoticeDepartmentDto;
import com.cg.entity.User;
import com.cg.utils.MailUtils;
import org.apache.rocketmq.spring.annotation.MessageModel;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Description 发送消息
 * @Author CG
 * @Date 2023/12/16 12:37
 **/
@Component
@RocketMQMessageListener(topic = "${topicName}",consumerGroup = "notice-group",messageModel = MessageModel.BROADCASTING)
public class NoticeMessageImpl implements RocketMQListener<NoticeDepartmentDto> {

    private final Logger logger = LoggerFactory.getLogger(NoticeMessageImpl.class);


    @Resource
    private MailUtils mailUtils;

    @Override
    public void onMessage(NoticeDepartmentDto noticeDepartmentDto) {
        logger.info("接收到消息"+noticeDepartmentDto);
        String subject = MailCommon.NOTICE_SUBJECT;
        //发送邮件
        StringBuffer stringBuffer = new StringBuffer();
        List<User> userList = noticeDepartmentDto.getUserList();
        if(userList != null){
            for (User user : userList) {
                stringBuffer.append(user.getUserName()+"您好：");
                stringBuffer.append(MailCommon.NOTICE_CONTENT_1);
                stringBuffer.append(noticeDepartmentDto.getNoticeTitle());
                stringBuffer.append(MailCommon.NOTICE_CONTENT_2);
                mailUtils.sendMail(subject,stringBuffer.toString(),user.getUserMail());
                logger.info("收件人为"+user.getUserMail());
            }

        }
        stringBuffer.setLength(0);
    }
}
